package com.sduept.nwld.dataserver.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.countanalyse.ProtectAnalysisManger;
import com.sduept.nwld.dataserver.manager.statistics.ElectricityGenerationManager;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;
import com.sduept.nwld.dataserver.model.countanalyse.ElectricityFault;
import com.sduept.nwld.dataserver.model.countanalyse.ElectricityInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 *发电机设备故障统计
 */
@RestController
@RequestMapping("/electricityGeneration")
public class ElectricityGenerationService {
	@Autowired
	private ProtectAnalysisManger pro;
	@Autowired
	private SuperDAO dao;
	@Autowired
	private FaultRecordQueryManager frqm;
	@Autowired
	private StatisticsCommonManager comM;
	@Autowired
	private ElectricityGenerationManager generationM;
	

	@GetMapping("/getElectricityFaultByYear/{year}")
	public String getElectricityFaultByYear1(@PathVariable("year") String year){
		List<?> list = pro.getElectricityFaultByYear(year);
		 Set<String> areas = new LinkedHashSet<String>();//公司名
		 Set<String> types = new LinkedHashSet<String>();//故障原因
		 for (Object object : list) {
			 Object [] objs = (Object[]) object;
			 types.add(objs[0].toString());
			 areas.add(objs[1].toString());
		}
		 StringBuffer buff = new StringBuffer();
		 buff.append("[{faultType:[");
		 for (String string : types) {
			 buff.append("'"+string+"',");
		}
		 buff.append("]},");
		 
		for (String area : areas) {
			buff.append("{name:'" + area + "',value:[");
			for (String type : types) {
				for (Object object : list) {
					Object[] objs = (Object[]) object;
					if(objs[0].equals(type)){
						if(area.equals(objs[1])){
							buff.append(objs[2] + ",");
							break;
						}else if (object.equals(list.get(list.size() - 1))) {
							buff.append(0 + ",");
						}
					}else if (object.equals(list.get(list.size() - 1))){
						buff.append(0 + ",");
					}
				}
			}
			buff.append("]},");
		}
		 
		 buff.append("]");
		return JSONObject.parse(buff.toString()).toString();
	}
	

	@GetMapping("/getElectricityFaultCount")
	@Deprecated
	public String getElectricityFaultCount1(){
		List<String> years = (List<String>) dao.nativeQuery("select DISTINCT year from t_count_electricity_generator_info order by year desc");
		List<Object> jsonList = new ArrayList<Object>();
		for (String year : years) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<Object> list = new ArrayList<Object>();
			Map<String, String> allMap = new HashMap<String, String>();
			Map<String, String> brokenMap = new HashMap<String, String>();
//			 List<ElectricityInfo> le = dao.findByCondition("year = '"+year+"'", ElectricityInfo.class);
			 List<ElectricityInfo> le = dao.findByCondition(ElectricityInfo.class, "year = ?", year);
			 int faultCounts = 0;
			 int counts = 0;
//			 String sql = "fault_time >= '"+year+"-01-01 00:00:00' and fault_time <'"+year+"-12-31 11:59:59'";
//			 List<ElectricityFault> lef =  dao.findByCondition(sql, ElectricityFault.class);
			 List<ElectricityFault> lef =  dao.findByCondition(ElectricityFault.class, "fault_time >= ? and fault_time < ?", year+"-01-01 00:00:00",year+"-12-31 11:59:59");
			 if(lef.size() > 0){
				 faultCounts = lef.size();
			 }
			if(le.size() ==1){
				counts = le.get(0).getCount();
			}
			allMap.put("name", "发电机台数");
			allMap.put("value", counts + "");
			brokenMap.put("name", "故障次数");
			brokenMap.put("value", faultCounts+"");
			list.add(allMap);
			list.add(brokenMap);
			map.put("name", year);
			map.put("value", list);
			jsonList.add(map);
		}
		Collections.reverse(jsonList);
		String str = JSONObject.toJSONString(jsonList);
		return str;
	}
	

	@GetMapping("/getElectricityFaultCountData/{startYear}/{endYear}/{voltage}")
	public String getElectricityFaultCountData(@PathVariable("startYear") String startYear,@PathVariable("endYear") String endYear,@PathVariable("voltage") String voltage){
		List<Object> jsonList = new ArrayList<Object>();
		List<FaultRecord> faultRecords = comM.getFaultRecordsByCondition(startYear, endYear, voltage, "发电机");
		Map<Integer, List<FaultRecord>> yearMap = comM.genFaultRecordMapByYear(faultRecords);
		comM.completeMapByYear(startYear, endYear, yearMap);
		Integer vol = null;
		if(!"0".equals(voltage)) {
			vol = Integer.parseInt(voltage);
		}
		for (Integer year : yearMap.keySet()) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<Object> list = new ArrayList<Object>();
			Map<String, String> allMap = new HashMap<String, String>();
			Map<String, String> brokenMap = new HashMap<String, String>();
			List<FaultRecord> faults = yearMap.get(year);
			// 获取年、电压等级下发电机台数
			List<ElectricityInfo> electricityInfos = generationM.getElectricityInfos(year, vol);
			ElectricityInfo info = new ElectricityInfo();
			if(electricityInfos.size()>0) {
				info = electricityInfos.get(0);
			} else {
				info.setCount(0);
			}
			allMap.put("name", "发电机台数");
			allMap.put("value", info.getCount() + "");
			brokenMap.put("name", "故障次数");
			brokenMap.put("value", faults.size()+"");
			list.add(allMap);
			list.add(brokenMap);
			map.put("name", year);
			map.put("value", list);
			jsonList.add(map);
		}
		Collections.reverse(jsonList);
		String str = JSONObject.toJSONString(jsonList);
		return str;
	}
	
}
